//
//  TapMoveView.swift
//  FirstSwiftProduct
//
//  Created by 范泽华 on 2017/11/10.
//  Copyright © 2017年 范泽华. All rights reserved.
//

import UIKit

@objc protocol tapEndDelegate {
    func tapEndAction() -> Void
}

class TapMoveView: UIView {

    weak var delegate:tapEndDelegate?
    
    var LastPoint :CGPoint!
    var _imageArr :NSArray!
    var imageArr: NSArray {
        get {
            return _imageArr
        }
        set {
            _imageArr = newValue
            reloadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)

    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touchesBegan")
        let touch = touches.first
        let point = touch?.location(in: self)
        LastPoint = point
        self.superview?.bringSubview(toFront: self)
    }
    
    
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if !self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01 {
            return nil
        }
        
        if self.point(inside: point, with: event) {
            for subView:UIView in self.subviews{
                
                let convertedPoint = subView.convert(point, from: self)
                let hitView = subView.hitTest(convertedPoint, with: event)
                if hitView != nil {
                    return hitView
                }
                
            }
            return self;
        }
        return nil
    }
    
    
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        
//        //计算位移=当前位置-起始位置
//        CGPoint point = [[touches anyObject] locationInView:self];
//        float dx = point.x - startPoint.x;
//        float dy = point.y - startPoint.y;
//
//        //计算移动后的view中心点
//        CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);
//
//
//
//        float halfx = CGRectGetMidX(self.bounds);
//        //x坐标左边界
//        newcenter.x = MAX(halfx, newcenter.x);
//        //x坐标右边界
//        newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x);
//
//        //y坐标同理
//        float halfy = CGRectGetMidY(self.bounds);
//        newcenter.y = MAX(halfy, newcenter.y);
//        newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y);
//
//        //移动view
//        self.center = newcenter;
        
        let point = touches.first?.location(in: self)
        let dx = point?.x
        let dy = point?.y
        
        let newCenter = CGPoint(x: self.center.x + dx!, y: self.center.y + dy!)
        
        
//        let point = touch?.location(in: self)
//        LastPoint = point;
        self.center = newCenter
//        print(point!)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        
//        delegate?.tapEndAction()
    }
    
    func reloadData() -> Void {
//        let rowCount:CGFloat = 3
//        let labelWidth = frame.size.width / (rowCount + 1)
//
//
//        for i in 0...(imageArr.count - 1) {
//            let row = i / Int(rowCount)
//            let line = i % Int(rowCount)
//
//            let label = UILabel.init(frame: CGRect(x: 10 + line * Int(labelWidth + 10), y: 20 + row * Int(40), width: Int(labelWidth), height: 30))
//            label.backgroundColor = UIColor.red
//            let sss = imageArr[i]
//            label.textAlignment = NSTextAlignment.center
//            label.text = sss as? String
//
//            label.addGestureRecognizer(<#T##gestureRecognizer: UIGestureRecognizer##UIGestureRecognizer#>)
//
//            self.addSubview(label)
//        }
    }
//    override func layoutSubviews() {
//        super.layoutSubviews()
//
//    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
